package com.geen.tuoke.ui.activity

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import androidx.lifecycle.Observer
import com.geen.tuoke.R
import com.geen.tuoke.base.BaseActivity
import com.geen.tuoke.databinding.ActivityLoginBinding
import com.geen.tuoke.ui.viewmodel.LoginViewModel
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.seeway.wms.utils.startActivity

/**
 * @Author LuoJi
 * @Date 2022/1/17-15:31
 * @Desc
 */
class LoginActivity :BaseActivity<LoginViewModel,ActivityLoginBinding>(),LoginViewModel.LoginHandler {

    var showPwd :Boolean = false

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        with(mBinding){
            lifecycleOwner = this@LoginActivity
            handler = this@LoginActivity
            viewModel = mViewModel
        }
        initView()

        mViewModel.loginSuccess.observe(this, {
            if(it){
                startActivity<MainActivity>()
                finish()
            }
        })
    }

    private fun initView(){
        QMUIStatusBarHelper.setStatusBarLightMode(this)
        mBinding.run {
            tvPwd.isSelected = true
            tvMessage.isSelected = false
        }
    }

    override fun clickShowPwd(view: View) {
        if(showPwd){
            showPwd = false
            mBinding.ivShowPwd.setImageResource(R.mipmap.icon_show_pwd)
            mBinding.editPwd.transformationMethod = PasswordTransformationMethod.getInstance()
        }else{
            showPwd = true
            mBinding.ivShowPwd.setImageResource(R.mipmap.icon_show_pwd_show)
            mBinding.editPwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
        }
    }

    override fun clickSwitchMessage(view: View) {
        mBinding.tvPwd.isSelected = false
        mBinding.tvMessage.isSelected = true
    }

    override fun clickSwitchPwd(view: View) {
        mBinding.tvPwd.isSelected = true
        mBinding.tvMessage.isSelected = false
    }

    override fun login(view: View) {
        mViewModel.loginAccount()
    }
}